/* Copyright 2009 Requirements Management System    
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.exception;

import org.apache.log4j.Logger;   
import org.mags.exception.common.AbstractExceptionKeyCreator;
import org.mags.exception.common.ExceptionHandler;
import org.mags.exception.common.ExceptionKeyCreator;
import org.mags.remas.view.dialogs.MessageDialog;
import org.mags.remas.view.dialogs.TextAreaMessageDialog;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class SingletonExceptionHandler extends ExceptionHandler {
	private static SingletonExceptionHandler instance = null;
	transient private Logger logger;
	private Logger getLogger() {
		if(logger==null)
			logger=Logger.getLogger(SingletonExceptionHandler.class);
		return logger;
	}		
	public static SingletonExceptionHandler getInstance() {
		if(instance==null)
			instance = new SingletonExceptionHandler();
		return instance;
	}
	private SingletonExceptionHandler() {
		super.setMessageCreator(new MessageCreator());
		AbstractExceptionKeyCreator keyCreator = new DerbyExceptionKeyCreator();
		keyCreator.setOrder(0);
		super.addExceptionKeyCreator(keyCreator);
		keyCreator = new ParseExceptionKeyCreator();
		keyCreator.setOrder(1);
		super.addExceptionKeyCreator(keyCreator);
		super.addExceptionKeyCreator(new ExceptionKeyCreator());
	}
	public void showError(String message) {
		getLogger().error(message);
		MessageDialog.openError(null, "error...", message);
	}
	public void showWarning(Throwable exception) {
		String message = super.getMessage(exception);
		if(!message.endsWith("."))
			message = message + ".";		
		getLogger().warn(message, exception);
		MessageDialog.openWarning(null, "warning...", message+" "+"Try again!");
	}
	public void showException(Throwable exception) {
		String message = super.getMessage(exception);
		getLogger().error(message, exception);		
		if(exception instanceof FileMessageException)
			TextAreaMessageDialog.openError(null, "error...", message, ((FileMessageException)exception).getContentFile(), exception);
		else
			MessageDialog.openError(null, "error...", message, exception);
	}	
	public boolean showExceptionAndQuestion(Throwable exception, String question) {
		String message = super.getMessage(exception);
		if(!message.endsWith("."))
			message = message + ".";
		getLogger().info(message, exception);
		if(exception instanceof FileMessageException)
			return TextAreaMessageDialog.openErrorAndQuestion(null, "error...", message+" "+question, ((FileMessageException)exception).getContentFile(), exception);
		else
			return MessageDialog.openErrorAndQuestion(null, "error...", message+" "+question, exception);	
		
	}
}
